home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ADA / GNAT / !gcc / adainc / 4 / ads / g-htable < prev    next >
Text File  |  1996-02-12  |  6KB  |  141 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                          G N A T . H T A B L E                           --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.4 $                              --
  10. --                                                                          --
  11. --     Copyright (C) 1992,1993,1994,1995 Free Software Foundation, Inc.     --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. package Gnat.Htable is
  37.  
  38.    -------------------
  39.    -- Simple_Htable --
  40.    -------------------
  41.  
  42.    --  A simple Hash-Table abstraction, easy to instanciate, easy to use.
  43.    --  The table associates one element to one key with the procedure SET.
  44.    --  GET retreives the Element stored for a given Key. The efficiency of
  45.    --  retreival is function of the size of the Table parameterized by
  46.    --  Header_Num and the hashing function Hash.
  47.  
  48.    generic
  49.       type Header_Num is range <>;
  50.       --  An integer type indicating the number and range of hash headers.
  51.  
  52.       type Element is private;
  53.       --  The type of element to be stored
  54.  
  55.       No_Element : Element;
  56.       --  The object that is returned by Get when no element has been set for
  57.       --  a given key
  58.  
  59.       type Key is private;
  60.       with function Hash  (F : Key)      return Header_Num;
  61.       with function Equal (F1, F2 : Key) return Boolean;
  62.  
  63.    package Simple_Htable is
  64.  
  65.       procedure Set (K : Key; E : Element);
  66.       --  Associate an element with a given key. Overrides any previously
  67.       --  associated element.
  68.  
  69.       function  Get (K : Key) return Element;
  70.       --  Returns the Element associated wtih a key or No_Element if the
  71.       --  given key has not associated element
  72.  
  73.    end Simple_Htable;
  74.  
  75.    -------------------
  76.    -- Static_Htable --
  77.    -------------------
  78.  
  79.    --  A low-level Hash-Table abstraction, not as easy to instanciate as
  80.    --  Simple_Htable but designed to allow complete control over the
  81.    --  allocation of necessary data structures. Particularly useful when
  82.    --  dynamic allocation is not desired. The model is that "Element"
  83.    --  contains its own Key that can be retreive by "Get_Key", furthermore,
  84.    --  "Element" provides a link that can be used by the Htable for linking
  85.    --   elements with same hash codes:
  86.  
  87.    --       Element
  88.  
  89.    --         +-------------------+
  90.    --         |       Key         |
  91.    --         +-------------------+
  92.    --         :   other data      :
  93.    --         +-------------------+
  94.    --         |     Next Elmt     |
  95.    --         +-------------------+
  96.  
  97.    generic
  98.       type Header_Num is range <>;
  99.       --  An integer type indicating the number and range of hash headers.
  100.  
  101.       type Element is limited private;
  102.       --  The type of element to be stored
  103.  
  104.       type Elmt_Ptr is access all Element;
  105.       with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
  106.       with function  Next     (E : Elmt_Ptr) return Elmt_Ptr;
  107.       --  The type must provide an internal link for the sake of the
  108.       --  staticness of the Htable.
  109.  
  110.       type Key is limited private;
  111.       with function Get_Key (E : Elmt_Ptr) return Key;
  112.       with function Hash    (F : Key)      return Header_Num;
  113.       with function Equal   (F1, F2 : Key) return Boolean;
  114.  
  115.    package Static_Htable is
  116.  
  117.       procedure Set (E : Elmt_Ptr);
  118.       --  Insert the element pointer in the Htable
  119.  
  120.       function  Get (K : Key) return Elmt_Ptr;
  121.       --  Returns the latest inserted element pointer with the given Key
  122.       --  or null if none.
  123.  
  124.       procedure Remove (K : Key);
  125.       --  Remove the latest inserted element pointer associated with the
  126.       --  given key if any, does nothing if none.
  127.  
  128.    end Static_Htable;
  129.  
  130.    ----------
  131.    -- Hash --
  132.    ----------
  133.  
  134.    --  A generic hashing function working on String keys
  135.  
  136.    generic
  137.       type Header_Num is range <>;
  138.    function Hash (Key : String) return Header_Num;
  139.  
  140. end Gnat.Htable;
  141.